##################################################################################################
## Replication Material  #########################################################################
## Article: "Government Dominance and the Role of Opposition in Parliamentary Democracies" #######
## Authors: Thomas König, Nick Lin, Thiago N. Silva    ###########################################
## European Journal of Political Research   ######################################################
## File: KLS_EJPR_Replication.R  #################################################################
##################################################################################################

## Installing packages
#install.packages(c("ggplot2", "gmodels", "ggeffects","gridExtra", "stargazer"))

## Loading packages
library(ggplot2)
library(gmodels)
library(ggeffects)
library(gridExtra)
library(stargazer)

##Loading Data
load(file = "~/Dropbox/Opposition_Chair/EJPR_Dataverse/KLS_final_data.Rdata")

## Main models
# "Figure 3: Predicted Probabilities of Amendments by 
# Opposition Support, Opposition Chair, and Bill-Chair 
# Policy Disagreement (Model 1 of Table B1)"
model1 <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                Opposition_Support*Opposition_Chair + Opposition_Support*Bill_Chair_Disagreement + 
                Opposition_Chair*Bill_Chair_Disagreement +
                Bill_Chair_Disagreement*Opposition_Chair*Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands,
              family="binomial", data = kls_final)
summary(model1)

figure_3 <- ggpredict(model1, c("Bill_Chair_Disagreement[all]", "Opposition_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_3.png", 
     units="in", width=7, height=4, res=900)
plot(figure_3, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.8)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

## "Figure 4: Predicted Probabilities of Amendments by 
## Opposition Support, Partner Chair,and Bill-Chair 
## Policy Disagreement (Model 2 of Table B1)"
model2 <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                Opposition_Support*Partner_Chair + Opposition_Support*Bill_Chair_Disagreement + 
                Partner_Chair*Bill_Chair_Disagreement + 
                Bill_Chair_Disagreement*Partner_Chair*Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands,
              family="binomial", data = kls_final)
summary(model2)

figure_4 <- ggpredict(model2, c("Bill_Chair_Disagreement[all]", "Partner_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_4.png", 
     units="in", width=7, height=4, res=900)
plot(figure_4, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.8)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()



#### CODES for the SUPPLEMENTARY MATERIAL

## Appendx A. Descriptive Statistics

## Table A1: Rates of Bill Passage in Denmark, Germany and the Netherlands
CrossTable(kls_final$billPassage[kls_final$country=="Denmark"])
CrossTable(kls_final$billPassage[kls_final$country=="Germany"])
CrossTable(kls_final$billPassage[kls_final$country=="Netherlands"])

## Table A2: Variables and Descriptive StatisticsTable A2:  Variables and Descriptive Statistics
stargazer(kls_final[c("Amendment", "Opposition_Chair", "Partner_Chair", 
                      "Bill_Chair_Disagreement", "Moderate_Bill_Chair_Disagreement",
                      "Opposition_Support_Numeric", "Chair_Seat_Share", "Number_Reviewing_Committees",
                      "Minority_Government", 
                      "Denmark", "Germany", "Netherlands")], 
          covariate.labels = c("Amendment Proposal", "Opposition Chair", "Partner Chair", 
                               "Bill-Chair Disagreement", "Moderate Bill-Chair Disagreement",
                               "Opposition Support", "Chair Party's Seat Share", 
                               "Number of Reviewing Committees",
                               "Minority Government", 
                               "Denmark", "Germany", "The Netherlands"),
          type = "text", digits=2, 
          summary.stat = c("mean", "sd", "min", "max", "n"))


## Table A3:  Rates of Amendment Proposals by Bill Passage in Denmark, Germany and the Netherlands
# Passed bills:
CrossTable(kls_final$Amendment[kls_final$country=="Denmark" & kls_final$billPassage==1])
CrossTable(kls_final$Amendment[kls_final$country=="Germany" & kls_final$billPassage==1])
CrossTable(kls_final$Amendment[kls_final$country=="Netherlands" & kls_final$billPassage==1])

# Failed bills:
CrossTable(kls_final$Amendment[kls_final$country=="Denmark" & kls_final$billPassage==0])
CrossTable(kls_final$Amendment[kls_final$country=="Germany" & kls_final$billPassage==0])
CrossTable(kls_final$Amendment[kls_final$country=="Netherlands" & kls_final$billPassage==0])

## Table A4: Rates of Amendment Proposals by Either Opposition, Coalition Partner, or Minister Chairmanship
#Denmark (529, 382, 244)
CrossTable(kls_final$Amendment[kls_final$country=="Denmark" & kls_final$Opposition_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Denmark" & kls_final$Partner_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Denmark" & kls_final$Opposition_Chair==0 & kls_final$Partner_Chair==0])

#Germany (501, 647, 261)
CrossTable(kls_final$Amendment[kls_final$country=="Germany" & kls_final$Opposition_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Germany" & kls_final$Partner_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Germany" & kls_final$Opposition_Chair==0 & kls_final$Partner_Chair==0])

#The Netherlands (345, 464, 291)
CrossTable(kls_final$Amendment[kls_final$country=="Netherlands" & kls_final$Opposition_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Netherlands" & kls_final$Partner_Chair==1])
CrossTable(kls_final$Amendment[kls_final$country=="Netherlands" & kls_final$Opposition_Chair==0 & kls_final$Partner_Chair==0])

### Appendix B. Main Results
## Table B1:  The Effects of Opposition Support on Government Bill Challenge
stargazer(model1, model2, type = "text")


## Appendix C. The Effect of Committee Chairmanship and 
## Opposition Support on BillChallenges, Conditional on 
## Compromise-Chair Distance 

model1c <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Moderate_Bill_Chair_Disagreement + 
                 Opposition_Support*Opposition_Chair + Opposition_Support*Moderate_Bill_Chair_Disagreement + 
                 Opposition_Chair*Moderate_Bill_Chair_Disagreement +
                 Moderate_Bill_Chair_Disagreement*Opposition_Chair*Opposition_Support  + 
                 Chair_Seat_Share  + Number_Reviewing_Committees +  
                 Minority_Government + Germany + Netherlands,
               family="binomial", data = kls_final)
summary(model1c)

model2c <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Moderate_Bill_Chair_Disagreement + 
                 Opposition_Support*Partner_Chair + Opposition_Support*Moderate_Bill_Chair_Disagreement + 
                 Partner_Chair*Moderate_Bill_Chair_Disagreement + 
                 Moderate_Bill_Chair_Disagreement*Partner_Chair*Opposition_Support  + 
                 Chair_Seat_Share + Number_Reviewing_Committees +  
                 Minority_Government  + Germany + Netherlands,
               family="binomial", data = kls_final)
summary(model2c)

## Table C1:  The Effects of Opposition Support on 
## Government Bill ChallengeRobustness Check:  
## Compromise Bill
stargazer(model1c, model2c, type="text")

## Figure C1:  Predicted Probabilities of Amendments by 
## Opposition Support, OppositionChair, and Compromise-Chair 
## Policy Disagreement (Model 1 of Table C1)
figure_c1 <- ggpredict(model1c, c("Moderate_Bill_Chair_Disagreement[all]", "Opposition_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                       ci.lvl = 0.95) 
png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_C1.png", 
     units="in", width=7, height=4, res=900)
plot(figure_c1, colors="bw") + labs(
  x="Moderate Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.2, 0.9)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

figure_c2 <- ggpredict(model2c, c("Moderate_Bill_Chair_Disagreement[all]", "Partner_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                       ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_C2.png", 
     units="in", width=7, height=4, res=900)
plot(figure_c2, colors="bw") + labs(
  x="Moderate Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.2, 0.9)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

### Appendix D. The Effect of Committee Chairmanship and 
### Distance to the Chair (With-out Opposition Support)

## Figure D1:  Predicted Probabilities of Amendments by 
## Committee Chair [a.  Oppositionor b.  Partner] and 
## Bill-Chair Policy Disagreement (Model 1 of Table B1)
pred_d1_1 <- ggpredict(model1, c("Bill_Chair_Disagreement[all]", "Opposition_Chair[1]"), 
                         ci.lvl = 0.95) 
fig_d1_1 <- plot(pred_d1_1, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title="a. Opposition Chair x Bill-Chair Disagreement"
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=10))

pred_d1_2 <- ggpredict(model2, c("Bill_Chair_Disagreement[all]", "Partner_Chair[1]"), 
                         ci.lvl = 0.95) 
fig_d1_2 <- plot(pred_d1_2, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title="a. Partner Chair x Bill-Chair Disagreement"
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=10))

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_D1.png", 
     units="in", width=7, height=4, res=900)
grid.arrange(fig_d1_1, fig_d1_2, ncol=2)
dev.off()

## Figure D2:  Predicted Probabilities of Amendments by 
## Committee Chair [a.  Oppositionor b.  Partner] and 
## Moderate Bill-Chair Policy Disagreement (Model 2 of Table B1)
pred_d2_1 <- ggpredict(model1c, c("Moderate_Bill_Chair_Disagreement[all]", "Opposition_Chair[1]"), 
                         ci.lvl = 0.95) 

fig_d2_1 <- plot(pred_d2_1, colors="bw") + labs(
  x="Moderate Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title="Opposition Chair x Moderate Bill-Chair Disagreement"
) + ylim(c(.2, 0.9)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=10))

pred_d2_2 <- ggpredict(model2c, c("Moderate_Bill_Chair_Disagreement[all]", "Partner_Chair[1]"), 
                         ci.lvl = 0.95) 

fig_d2_2 <- plot(pred_d2_2, colors="bw") + labs(
  x="Moderate Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title="Partner Chair x Moderate Bill-Chair Disagreement"
) + ylim(c(.2, 0.9)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=10))

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_D2.png", 
     units="in", width=7, height=4, res=900)
grid.arrange(fig_d2_1, fig_d2_2, ncol=2)
dev.off()

### Appendix E. The Effect of Committee Chairmanship and Opposition 
### Support on BillChallenges,  Conditional on Minister-Chair 
### Distance.  Robustness Check:Policy Areas Fixed-Effects
model1_pol <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                Opposition_Support*Opposition_Chair + Opposition_Support*Bill_Chair_Disagreement + 
                Opposition_Chair*Bill_Chair_Disagreement +
                Bill_Chair_Disagreement*Opposition_Chair*Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands  + 
                  Economic_Policy_Area + Environment_Policy_Area + 
              Foreign_Policy_Area + Social_Policy_Area,
              family="binomial", data = kls_final)
summary(model1_pol)

model2_pol <- glm(Amendment ~ Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                Opposition_Support*Partner_Chair + Opposition_Support*Bill_Chair_Disagreement + 
                Partner_Chair*Bill_Chair_Disagreement + 
                Bill_Chair_Disagreement*Partner_Chair*Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands  + 
                  Economic_Policy_Area + Environment_Policy_Area + 
                Foreign_Policy_Area + Social_Policy_Area,
              family="binomial", data = kls_final)
summary(model2_pol)

## Table E1:  The Effects of Opposition Support on Government Bill 
## ChallengeRobustness Check: Policy Areas Fixed-Effects
stargazer(model1_pol, model2_pol, type="text")


## Figure E1:  Predicted Probabilities of Amendments by 
## Opposition Support, OppositionChair, and Minister-Chair 
## Policy Disagreement.  Robustness Check:  Policy Areas 
## Fixed-Effects.  (Model 1 of Table E1)
predi_e1 <- ggpredict(model1_pol, c("Bill_Chair_Disagreement[all]", "Opposition_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_E1.png", 
     units="in", width=7, height=4, res=900)
plot(predi_e1, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

## Figure E2:  Predicted Probabilities of Amendments by 
## Opposition Support, PartnerChair, and Minister-Chair 
## Policy Disagreement.  Robustness Check:  Policy Areas
## Fixed-Effects.  (Model 2 of Table E1)
predi_e2 <- ggpredict(model2_pol, c("Bill_Chair_Disagreement[all]", "Partner_Chair[1]", "Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_E2.png", 
     units="in", width=7, height=4, res=900)
plot(predi_e2, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

### Appendix F. The Effect of Committee Chairmanship and 
### Opposition Support on Bill Challenges, Conditional on 
### Bill-Chair Disagreement.  Robustness Check:
### Opposition Support as a Continuous Variable
model1_cont <- glm(Amendment ~ Opposition_Support_Continuous + Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                    Opposition_Support_Continuous*Opposition_Chair + Opposition_Support_Continuous*Bill_Chair_Disagreement + 
                    Opposition_Chair*Bill_Chair_Disagreement +
                    Bill_Chair_Disagreement*Opposition_Chair*Opposition_Support_Continuous  + 
                    Chair_Seat_Share  + Number_Reviewing_Committees +  
                    Minority_Government  + Germany + Netherlands,
                  family="binomial", data = kls_final)
summary(model1_cont)

model2_cont <- glm(Amendment ~ Opposition_Support_Continuous+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                    Opposition_Support_Continuous*Partner_Chair + Opposition_Support_Continuous*Bill_Chair_Disagreement + 
                    Partner_Chair*Bill_Chair_Disagreement + 
                    Bill_Chair_Disagreement*Partner_Chair*Opposition_Support_Continuous + 
                    Chair_Seat_Share  + Number_Reviewing_Committees +  
                    Minority_Government  + Germany + Netherlands,
                  family="binomial", data = kls_final)
summary(model2_cont)

## Table F1: The Effects of Opposition Support on Government 
## Bill ChallengeRobustness Check:  Opposition Support as a 
## Continuous Variable(From High Support to Low Support)
stargazer(model1_cont, model2_cont, type="text")

## Figure F1: Predicted Probabilities of Amendments by Opposition 
## Support, Opposition Chair, and Minister-Chair Policy Disagreement.  
## Robustness Check:  Opposition Supportas a Continuous Variable 
## (From High Support [2.72] to Low Support [9.11]).  (Model 1 ofTable F1)
pred_f1 <- ggpredict(model1_cont, c("Bill_Chair_Disagreement[all]", "Opposition_Chair[1]", "Opposition_Support_Continuous"), 
                          ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_F1.png", 
     units="in", width=7, height=4, res=900)
plot(pred_f1, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

## Figure F2:  Predicted Probabilities of Amendments by Opposition 
## Support, Partner Chair, and Minister-Chair Policy Disagreement.  
## Robustness Check:  Opposition Support as a Continuous Variable 
## (From High Support [2.72] to Low Support [9.11]). (Model 2 of Table F1)
predi_f2 <- ggpredict(model2_cont, c("Bill_Chair_Disagreement[all]", "Partner_Chair[1]", "Opposition_Support_Continuous"), 
                          ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_F2.png", 
     units="in", width=7, height=4, res=900)
plot(predi_f2, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

### Appendix G. The Effect of Committee Chairmanship and Opposition 
### Support on Bill Challenges, Conditional on Minister-Chair Distance.  
## Robustness Check: Alternative Measurement of Opposition Support
model1_alt <- glm(Amendment ~ Alternative_Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                    Alternative_Opposition_Support*Opposition_Chair + Alternative_Opposition_Support*Bill_Chair_Disagreement + 
                Opposition_Chair*Bill_Chair_Disagreement +
                Bill_Chair_Disagreement*Opposition_Chair*Alternative_Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands,
              family="binomial", data = kls_final)
summary(model1_alt)

model2_alt <- glm(Amendment ~ Alternative_Opposition_Support+ Opposition_Chair + Partner_Chair + Bill_Chair_Disagreement + 
                    Alternative_Opposition_Support*Partner_Chair + Alternative_Opposition_Support*Bill_Chair_Disagreement + 
                Partner_Chair*Bill_Chair_Disagreement + 
                Bill_Chair_Disagreement*Partner_Chair*Alternative_Opposition_Support  + 
                Chair_Seat_Share  + Number_Reviewing_Committees +  
                Minority_Government  + Germany + Netherlands,
              family="binomial", data = kls_final)
summary(model2_alt)

## Table G1: The Effects of Opposition Support on Government Bill 
## Challenge. Robustness Check:  Alternative Measurement of Opposition Support
stargazer(model1_alt, model2_alt, type="text")

## Figure G1:  Predicted Probabilities of Amendments by Opposition 
## Support, OppositionChair, and Minister-Chair Policy Disagreement.  
## Robustness Check:  AlternativeMeasurement of Opposition Support.  
## (Model 1 of Table G1
predi_g1 <- ggpredict(model1_alt, c("Bill_Chair_Disagreement[all]", "Opposition_Chair[1]", "Alternative_Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_G1.png", 
     units="in", width=7, height=4, res=900)
plot(predi_g1, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()

## Figure G2: Predicted Probabilities of Amendments by 
## Opposition Support, PartnerChair, and Minister-Chair 
## Policy Disagreement.  Robustness Check:  Alternative Measurement of 
## Opposition Support.  (Model 2 of Table G1)
predi_g2 <- ggpredict(model2_alt, c("Bill_Chair_Disagreement[all]", "Partner_Chair[1]", "Alternative_Opposition_Support[Low Opposition Support, High Opposition Support]"), 
                      ci.lvl = 0.95) 

png("~/Dropbox/Opposition_Chair/EJPR_Dataverse/Figure_G2.png", 
     units="in", width=7, height=4, res=900)
plot(predi_g2, colors="bw") + labs(
  x="Bill-Chair Disagreement",
  y="P(Amendment Proposals)",
  title=""
) + ylim(c(.3, 0.85)) +
  theme(legend.position="bottom") + theme(plot.title = element_text(size=12))
dev.off()